package com.dms.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dms.common.utils.DateUtils;
import com.dms.system.domain.SysMission;
import com.dms.system.domain.SysOmission;
import com.dms.system.domain.dto.SysTodayMissionDTO;
import com.dms.system.mapper.SysMissionMapper;
import com.dms.system.mapper.SysOmissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dms.system.mapper.SysTodayMissionMapper;
import com.dms.system.domain.SysTodayMission;
import com.dms.system.service.ISysTodayMissionService;
import com.dms.common.core.text.Convert;

/**
 * 每日任务Service业务层处理
 * 
 * @author xlong_zhu
 * @date 2021-04-05
 */
@Service
public class SysTodayMissionServiceImpl implements ISysTodayMissionService 
{
    @Autowired
    private SysTodayMissionMapper sysTodayMissionMapper;

    @Autowired
    private SysMissionMapper sysMissionMapper;

    @Autowired
    private SysOmissionMapper sysOmissionMapper;

    @Override
    public int batchInsertSysTodayMission() {
        // 1. 生成漏检统计
        // 查询每日任务表，返回未完成isComplete=0的所有任务对象
        List<SysOmission> omissionList = sysOmissionMapper.selectUnCompletedTodayMission();
        if(omissionList!=null && omissionList.size()!=0){
            // 将未完成的任务对象，批量插入到sys_omission表中
            sysOmissionMapper.batchInsertSysOmission(omissionList);
            // 截断今日任务表
            sysTodayMissionMapper.truncateTable();
        }
        // 2. 生成每日任务
        // 查询任务列表
        List<SysMission> sysMissionList = sysMissionMapper.selectSysMissionList(null);
        // 创建一个list，存放每日任务对象
        List<SysTodayMission> sysTodayMissionList = new ArrayList<>();
        // 遍历mission列表，创建今日任务对象，加入到list中
        for (SysMission mission : sysMissionList){
            switch (mission.getMissionCycle()){
                case 1:{
                    // 判断执行的那天是不是今天
                    if(DateUtils.isSameDay(mission.getMissionDate(),new Date())){
                        // 今天执行
                        add2list(mission,sysTodayMissionList);
                    }
                    break;
                }
                case 2:{
                    // 每天执行
                    add2list(mission,sysTodayMissionList);
                    break;
                }
                case 3:{
                    // 每周执行，判断今天是周几
                    if(DateUtils.getWeekDay().equals(mission.getMissionWeekday())){
                        add2list(mission,sysTodayMissionList);
                    }
                    break;
                }
                case 4:{
                    // 每月执行，判断今天是几号
                    if(DateUtils.getMonthDay().equals(mission.getMissionMonthday())){
                        add2list(mission,sysTodayMissionList);
                    }
                    break;
                }
                default:break;
            }
        }
        return sysTodayMissionMapper.batchInsertSysTodayMission(sysTodayMissionList);
    }

    /**
     * 将今天要执行的任务封装到SysTodayMission中
     * 然后添加到今日执行任务列表中
     * */
    public void add2list(SysMission mission,List<SysTodayMission> list){
        SysTodayMission sysTodayMission = new SysTodayMission();
        sysTodayMission.setMissionId(mission.getMissionId());
        sysTodayMission.setTodayStartTime(DateUtils.convert2Today(mission.getMissionStartTime()));
        sysTodayMission.setTodayEndTime(DateUtils.convert2Today(mission.getMissionEndTime()));
        sysTodayMission.setMissionIscomplete(0);
        list.add(sysTodayMission);
    }

    /**
     * 查询每日任务
     * 
     * @param todayMissionId 每日任务ID
     * @return 每日任务
     */
    @Override
    public SysTodayMission selectSysTodayMissionById(Long todayMissionId)
    {
        return sysTodayMissionMapper.selectSysTodayMissionById(todayMissionId);
    }

    /**
     * 查询每日任务列表
     * 
     * @param sysTodayMission 每日任务
     * @return 每日任务
     */
    @Override
    public List<SysTodayMissionDTO> selectSysTodayMissionList(SysTodayMissionDTO sysTodayMission)
    {
        return selectSysTodayMissionList(sysTodayMission,null);
    }

    @Override
    public List<SysTodayMissionDTO> selectSysTodayMissionList(SysTodayMissionDTO sysTodayMission, Long userId) {
        return sysTodayMissionMapper.selectSysTodayMissionDTOList(sysTodayMission,userId);
    }

    /**
     * 新增每日任务
     * 
     * @param sysTodayMission 每日任务
     * @return 结果
     */
    @Override
    public int insertSysTodayMission(SysTodayMission sysTodayMission)
    {
        return sysTodayMissionMapper.insertSysTodayMission(sysTodayMission);
    }

    /**
     * 修改每日任务
     * 
     * @param sysTodayMission 每日任务
     * @return 结果
     */
    @Override
    public int updateSysTodayMission(SysTodayMission sysTodayMission)
    {
        return sysTodayMissionMapper.updateSysTodayMission(sysTodayMission);
    }

    /**
     * 删除每日任务对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysTodayMissionByIds(String ids)
    {
        return sysTodayMissionMapper.deleteSysTodayMissionByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除每日任务信息
     * 
     * @param todayMissionId 每日任务ID
     * @return 结果
     */
    @Override
    public int deleteSysTodayMissionById(Long todayMissionId)
    {
        return sysTodayMissionMapper.deleteSysTodayMissionById(todayMissionId);
    }
}
